package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.Integral;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.service.IntegralService;
import com.hxgz.steel.util.CookieUtil;
import com.hxgz.steel.util.JsonUtil;
import com.hxgz.steel.util.RedisPoolUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("Integral")
@Api(tags = "IntegralController",description = "前台-积分相关")
public class IntegralController {

    @Autowired
    private IntegralService integralService;

    @RequestMapping(value = "getIntegralList.do",method = RequestMethod.POST)
    @ApiOperation("个人中心-我的积分")
    public ServerResponse<PageInfo<List<Integral>>> getIntegralList(HttpServletRequest httpServletRequest, Page page,
                                                          @ApiParam(value = "变动类型 0增加 1减少",required = true)@RequestParam Integer type){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return integralService.getIntegralList(user.getId(),page,type);
    }

    @ApiOperation("清空积分")
    @RequestMapping(value = "cleanIntegral.do",method = RequestMethod.POST)
    public ServerResponse cleanIntegral(HttpServletRequest httpServletRequest){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return integralService.cleanIntegral(user.getId());
    }

}
